'''
Created on Feb 24, 2009

@author: whaleyr
'''

import os
from models import Semester

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms

from django import http

class SemesterForm(djangoforms.ModelForm):
  '''
  Is this where the documenation goes?
  '''
  class Meta:
    model = Semester
    exclude = ['user','gpa','semesterHours']


def add(request):
  user = users.GetCurrentUser()
  semester = None
  if user is None:
    return http.HttpResponseForbidden('You must be signed in to add or edit a semester')  
  if not request.POST:
    return http.HttpNoResponseForbidden('No semester data supplied')
  form = SemesterForm(data=request.POST or None, instance=semester)
  errors = form.errors
  if not errors:
    try:
      semester = form.save(commit=False)
      semester.user = user
    except ValueError, err:
      errors['__all__'] = unicode(err)
  if errors:
    return http.HttpNoResponseForbidden('Error with semester data supplied')
  semester.put()
  return http.HttpResponseRedirect('/')

def delete(request, semester_id):
  user = users.GetCurrentUser()
  if semester_id:
    semester = Semester.get(db.Key.from_path(Semester.kind(),int(semester_id)))
    if (semester is None):
      return http.HttpResponseNotFound('No semester exists with key (%r)' % semester_id)
    semester.delete()
  return http.HttpResponseRedirect('/')  

def __init__(selfparams):
    '''
    Constructor
    '''
        